package Lianxi.ATM;

import MySql.Sql;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AtmSql {
    private Atm frame;
    Sql sql = new Sql();


    //调取数据
    public List query(Atm frame) {
        this.frame = frame;

        //创建连接
        Connection con = sql.getConnection();
        Statement statement = null;
        ResultSet rs = null;
        List<User> atmUser = new ArrayList();
        //查询调取
        try {
            statement = con.createStatement();
            String mysql = "select * from t_atm";
            rs = statement.executeQuery(mysql);

            while (rs.next()) {
                User sqlUser = new User();
                sqlUser.setId(rs.getString(1));
                sqlUser.setPassword(rs.getString(2));
                sqlUser.setMoney(rs.getInt(3));
                atmUser.add(sqlUser);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }
            }

        }
        return atmUser;


    }


    //修改保存数据
    public void insert(Atm frame) {
        this.frame = frame;
        Connection con = sql.getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();

            int money = frame.getLoginUser().getMoney();
            String id = frame.getLoginUser().getId();
            String pwd = frame.getLoginUser().getPassword();

            String mysql = "update t_atm set atm_money='" + money + "' where atm_id='" + id + "'";
            String mysql1 = "update t_atm set atm_pwd='" + pwd + "' where atm_id='" + id + "'";

            //修改执行
            statement.executeUpdate(mysql);
            statement.executeUpdate(mysql1);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            guanbi(con, statement);
        }


    }

    public static void guanbi(Connection con, Statement statement) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


}